# 基于官方的 PHP 8.3 Apache 镜像
FROM m.daocloud.io/docker.io/library/php:8.3-apache

# 安装系统依赖（合并为单层减少镜像体积）
RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libicu-dev \
    libldap2-dev \
    libgmp-dev \
    libxml2-dev \
    libzip-dev \
    libxslt-dev \
    libssl-dev \
    libcurl4-openssl-dev \
    zlib1g-dev \
    libsqlite3-dev \
    unixodbc-dev \
    libc-client-dev \
    libkrb5-dev \
    libonig-dev \
    libpq-dev \
    libsodium-dev \
    libevent-dev \
    libtidy-dev \
    libmongoc-dev \
    uuid-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 配置扩展
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ && \
    docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
    docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu

# 安装其他核心扩展（合并为单层）
RUN docker-php-ext-install \
    mbstring iconv sockets \
    pdo pdo_mysql mysqli \
    bcmath calendar ctype curl dom exif \
    gd opcache soap xsl zip \
    ftp pcntl shmop sysvmsg sysvsem sysvshm tidy intl

# 强制清理旧event扩展（避免缓存干扰）
RUN pecl uninstall event || true && \
    rm -f /usr/local/lib/php/extensions/no-debug-non-zts-*/event.so

# 安装event扩展并严格控制加载顺序

RUN pecl install event && \
    echo "extension=sockets.so" > /usr/local/etc/php/conf.d/00-sockets.ini && \
    echo "extension=event.so" > /usr/local/etc/php/conf.d/10-event.ini && \
    docker-php-ext-enable event --ini-name 10-event.ini

# 安装其他PECL扩展
RUN pecl install redis igbinary mongodb uuid && \
    docker-php-ext-enable redis igbinary mongodb uuid

# Apache配置
RUN a2enmod rewrite headers && \
    echo "ServerName localhost" >> /etc/apache2/apache2.conf

# PHP生产环境配置
RUN { \
    echo 'date.timezone = Asia/Shanghai'; \
    echo 'memory_limit = 512M'; \
    echo 'upload_max_filesize = 64M'; \
    echo 'post_max_size = 64M'; \
    echo 'opcache.enable=1'; \
    echo 'opcache.validate_timestamps=0'; \
} > /usr/local/etc/php/conf.d/production.ini

# 设置工作目录
WORKDIR /var/www/html

# Warning: Module "sockets" is already loaded in Unknown on line 0 解决办法
# extension=sockets 删除  /usr/local/etc/php/conf.d/docker-php-ext-sockets.ini